//
//  PhotoViewModel.m
//  zhuchiren
//
//  Created by rainday on 15/2/8.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "PhotoViewModel.h"
#import <AFNetworking.h>
#import "IP.h"

@implementation PhotoViewModel

- (id)init{
    self = [super init];
    if (self) {
        _dataArray = [NSMutableArray array];
        
    }
    return self;
}


- (void)fetchDatazWithUser:(NSString *)username{
    NSString *url = [NSString stringWithFormat:@"%s/photo",SERVER];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    id token = [userDefaults objectForKey:@"token"];
    
    NSDictionary *params;
    
    if (username !=nil) {
        params = @{@"username":username};
    }
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];

    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _dataArray = [responseObject objectForKey:@"photo"];
        self.hasLoaded = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (void)removeItemsInArray:(NSArray *)array{
    for (NSIndexPath *indexPath in array) {
        NSString *url = [NSString stringWithFormat:@"%s/removephoto",SERVER];
        id pk = [_dataArray[indexPath.row] objectForKey:@"pk"];
        NSDictionary *params = @{@"pk":pk};
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        id token = [userDefaults objectForKey:@"token"];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        
        [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    }
}


- (void)addPhoto:(id)photo{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id  token = [userDefaults objectForKey:@"token"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    
    
    NSString *url = [NSString stringWithFormat:@"%s/photo",SERVER];
    
    
    
    
    [manager POST:url  parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:UIImageJPEGRepresentation(photo, .5)
                                    name:@"photo"
                                fileName:[[self getName] stringByAppendingString:@".png"]
                                mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self fetchDatazWithUser:nil];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];


}

- (NSInteger)numberOfRowsInSection:(NSInteger)section{
    return [_dataArray count];
}

- (NSDictionary *)dictionaryForItemAtIndexPath:(NSIndexPath *)indexPath{
    return _dataArray[indexPath.row];
}

- (NSString *)getName{
    char data[18];
    for (int x=0;x<18;data[x++] = (char)('A' + (arc4random_uniform(26))));
    return [[NSString alloc] initWithBytes:data length:18 encoding:NSUTF8StringEncoding];
}
@end
